package com.qf.co.service;

import com.qf.co.Dao.UserDao;
import com.qf.co.Dao.UserDaoImpl;
import com.qf.co.pojo.User;
import com.qf.co.utils.Constants;
import com.qf.co.utils.MD5Utils;


/**
 * @author hanjiejie
 * @version V1.0
 * @Project java2203
 * @Package com.qf.co.service
 * @Description:
 * @Date 2022/5/27 13:49
 */
public class UserServiceImpl implements UserService {
    @Override
    public Boolean check(String username) {
        UserDao userDao = new UserDaoImpl();
        User user = userDao.findByUserName(username);
        if (user == null){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public int createUser(String username,String password) {
        User user = new User(null,username,password,1);
        //1.密码使用MD5加密
        String md5 = MD5Utils.md5(user.getPassword());
        user.setPassword(md5);
        //设置消费者状态  默认为激活状态
//        user.setStatus(1);
        //调用Dao层
        UserDao userDao = new UserDaoImpl();
        int count = userDao.AddUser(user);
        return count;
    }

    @Override
    public User login(String username, String password) {
        //调用Dao层
        UserDao userDao = new UserDaoImpl();
        User user = userDao.findByUserName(username);
        if (user == null) {
            return null;
        }
        //判断用户是否正常
        if (user.getStatus()!= Constants.ROLE_ADMIN) {
            return null;
        }

        //判断用户密码是否正确，使用md5将用户输入的密码加密将加密后的密文和数据库里面的比较是否正确
        String md5 = MD5Utils.md5(password);
        if (md5.equals(user.getPassword())) {
            return user;
        }
        return null;
    }
}